---
layout: post
title: "Using HTML5 localStorage Into Your App, Check ldb.js"
date: 2013-12-22
comments: false
categories:
 - html5
 - localStorage
---

<div class='post'>
<div dir="ltr" style="text-align: left;" trbidi="on">Last night, I was hacking on a small utility library for use when using HTML5 localStorage. When you work with localStorage, it often gets into repetitive state when use use same command again and again. This library just abstracts the process and makes things easier for you. It would work on all modern browsers.<br /><br /><pre><code class="javascript">/**<br /> * Ldb.js<br /> * Copyright(c)2013  Vinit Kumar <vinit .kumar="" changer.nl=""><br /> * MIT Licensed<br /> */<br /><br />var Ldb = function () {};<br /><br />/**<br /> * [check Checks for localStorage support on the browsers]<br /> * @return {[String]} [status of localStorage Support]<br /> */<br />Ldb.prototype.check = function () {<br />  if (!localStorage) {<br />    return 'Please use a Modern browser, your browser does not support localStorage yet';<br />  } else {<br />    return 'Bingo, your Browsers supports localStorage, phew!';<br />  }<br />};<br /><br />/**<br /> * [get Gives the data stored in the key]<br /> * @param  {[String]} key [Key to retrive the data stored in localStorage]<br /> * @return {[JSON]}     [The JSON data stored | Error message saying no data stored with that key]<br /> */<br />Ldb.prototype.get = function (key) {<br />  if (localStorage.getItem(key)) {<br />    return JSON.parse(localStorage.getItem(key));<br />  } else {<br />    return 'No such key exists, Are you sure you have saved data with the key?';<br />  }<br />};<br /><br />/**<br /> * [set Saved data in the localStorage database]<br /> * @param {[String]} key   []<br /> * @param {[JSON]} value [The JSON data that gets stored in the localStorage]<br /> */<br />Ldb.prototype.set = function(key, value) {<br />  if (typeof (value) === 'object') {<br />    var stringValue = JSON.stringify(value);<br />    localStorage.setItem(key, stringValue);<br />    return 'Successfully saved your data to the key' + key +' key!';<br />  }<br />};<br /><br />/**<br /> * [remove description]<br /> * @param  {[String]} key [Key for the data to be deleted]<br /> * @return {[String]}     [Status message for the remove operation]<br /> */<br />Ldb.prototype.remove = function(key) {<br />  if (localStorage.getItem(key)) {<br />    localStorage.removeItem(key);<br />    return 'Successfully removed data with the key'+ key +' !';<br />  } else {<br />    return 'No such key exists, nothing to remove';<br />  }<br />};<br /></vinit><br /></code><br /></pre><br />This is all code for the utility as of now. More features are going to flow in coming weeks. Also, you can just install it using 'npm install ldb' or 'bower install ldb.js'. The code is Open Sourced here.&nbsp;https://github.com/vinitkumar/ldb.js</div></div>
